package com.hongtech.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.hongtech.common.service.impl.CrudServiceImpl;
import com.hongtech.modules.system.dto.SysMenuDTO;
import com.hongtech.modules.system.entity.SysMenuEntity;
import com.hongtech.modules.system.entity.SysRoleMenuEntity;
import com.hongtech.modules.system.mapper.SysMenuMapper;
import com.hongtech.modules.system.mapper.SysRoleMenuMapper;
import com.hongtech.modules.system.service.SysMenuService;
import com.hongtech.modules.system.vo.TreeMenuAccessVO;
import com.hongtech.modules.system.vo.TreeMenuVO;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * SysMenu 表数据服务层接口实现类
 */
@Service
public class SysMenuServiceImpl extends CrudServiceImpl<SysMenuMapper, SysMenuEntity, SysMenuDTO>
        implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<String> listMenuIdsByUserId(Long uid) {
        if (uid == 1L) {
            List<SysMenuEntity> allMenus = sysMenuMapper.selectList(null);
            return allMenus.stream().map(SysMenuEntity::getPermissions).collect(Collectors.toList());
        } else {
            return sysMenuMapper.listMenuIdsByUserId(uid);
        }
    }

    @Override
    public List<TreeMenuVO> listTreeMenuByUserId(Long uid) {
        List<Long> menuIds;
        // 当前用户菜单权限
        if (uid == 1L) {
            List<SysMenuEntity> allMenus = sysMenuMapper.selectList(null);
            menuIds = allMenus.stream().map(SysMenuEntity::getId).collect(Collectors.toList());
        } else {
            List<SysRoleMenuEntity> sysRoleMenuEntities = sysRoleMenuMapper.listRoleMenuByUserId(uid);
            menuIds = sysRoleMenuEntities.stream().map(SysRoleMenuEntity::getMenuId).collect(Collectors.toList());
        }
        return listTreeMenuByMenuIdsAndPid(menuIds, 0L);
    }

    @Override
    public List<TreeMenuVO> listTreeMenuByMenuIdsAndPid(List<Long> menuIds, Long pid) {
        if (ObjectUtils.isEmpty(menuIds)) {
            return null;
        }
        LambdaQueryWrapper<SysMenuEntity> ew = new LambdaQueryWrapper<>();
        ew.eq(SysMenuEntity::getPid, pid);
        ew.in(SysMenuEntity::getId, menuIds);
        ew.orderByAsc(SysMenuEntity::getSort);
        List<SysMenuDTO> sysMenuEntities = this.list(ew);
        List<TreeMenuVO> treeMenus = Lists.newArrayList();
        for (SysMenuDTO sysMenuDTO : sysMenuEntities) {
            TreeMenuVO treeMenuVO = new TreeMenuVO();
            treeMenuVO.setSysMenu(sysMenuDTO);
            if (sysMenuDTO.getDeep() < 2) {
                treeMenuVO.setChildren(listTreeMenuByMenuIdsAndPid(menuIds, sysMenuDTO.getId()));
            }
            treeMenus.add(treeMenuVO);
        }
        return treeMenus;
    }

    @Override
    public List<TreeMenuAccessVO> listMenuByMenuIds(List<Long> menuIds, Long pid) {
        LambdaQueryWrapper<SysMenuEntity> menuWrapper = new LambdaQueryWrapper<>();
        menuWrapper.eq(SysMenuEntity::getPid, pid);
        menuWrapper.orderByAsc(SysMenuEntity::getSort);
        List<SysMenuDTO> sysMenuDTOs = this.list(menuWrapper);
        List<TreeMenuAccessVO> treeMenuAccess = Lists.newArrayList();
        for (SysMenuDTO sysMenu : sysMenuDTOs) {
            TreeMenuAccessVO treeMenuAccessVO = new TreeMenuAccessVO();
            treeMenuAccessVO.setSysMenu(sysMenu);
            // 是否有权限
            if (menuIds.contains(sysMenu.getId())) {
                treeMenuAccessVO.setAllowAccess(true);
            }
            // 子节点
            if (sysMenu.getDeep() < 3) {
                treeMenuAccessVO.setChildren(listMenuByMenuIds(menuIds, sysMenu.getId()));
            }
            treeMenuAccess.add(treeMenuAccessVO);
        }
        return treeMenuAccess;
    }

}
